A Vite plugin for analyzing GraphQL usage
A powerful tool for analyzing GraphQL operation usage in your codebase. Track which queries, mutations, and subscriptions from your GraphQL schema are actually implemented in your application.
npm install vite-plugin-graphql-usage --save-dev
# or
pnpm install vite-plugin-graphql-usage --save-dev
# or
yarn add vite-plugin-graphql-usage --dev
Add the plugin to your vite.config.ts
:
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react-swc';
import viteGraphQLUsages from 'vite-plugin-graphql-usage';
export default defineConfig({
plugins: [
react(),
viteGraphQLUsages({
schemaSource: {
endpoint: 'https://rickandmortyapi.com/graphql'
// or
// sdlPath: './schema.graphql'
},
include: ['**/*.ts', '**/*.tsx'],
exclude: ['node_modules/**', '**/*.test.*'],
outputFileName: 'graphql-usage-report.md',
saveReport: true, // Save markdown report (default: true)
printTable: true // Print detailed table in console (optional)
})
],
});
Run the analyzer from the command line:
# Using GraphQL endpoint
npx graphql-usage-analyzer analyze --endpoint https://rickandmortyapi.com/graphql
# Using SDL file
npx graphql-usage-analyzer analyze --sdl ./schema.graphql
# With custom options
npx graphql-usage-analyzer analyze \
--endpoint https://api.example.com/graphql \
--include "src/**/*.ts,src/**/*.tsx" \
--exclude "**/*.test.*,**/*.spec.*" \
--output ./reports/graphql-usage.md
Choose one of the following:
{
schemaSource: {
endpoint: 'https://api.example.com/graphql'
}
}
{
schemaSource: {
sdlPath: './schema.graphql'
}
}
Control which files to analyze:
{
include: ['**/*.ts', '**/*.tsx', '**/*.js', '**/*.jsx'],
exclude: ['node_modules/**', '**/*.test.*', '**/*.spec.*']
}
{
outputFileName: 'my-graphql-report.md',
queryDirectory: './src/graphql/**/*.ts',
saveReport: true, // Whether to save markdown report (default: true)
printTable: false, // Whether to print detailed table in console (default: undefined)
sortOrder: 'original' // Sort order: 'completed-first', 'uncompleted-first', or 'original' (default: 'original')
}
saveReport
: Controls whether a markdown report file is generated. Set to false
to disable file generation and only show console output.printTable
: Controls whether the detailed table is printed to the console. When true
, shows a comprehensive table with all operations, their status, file paths, and line numbers.viteGraphQLUsages({
schemaSource: {
endpoint: 'https://api.example.com/graphql'
},
include: ['src/**/*.ts', 'src/**/*.tsx'],
exclude: ['**/*.test.*', '**/*.spec.*'],
outputFileName: 'custom-report.md',
saveReport: true, // Generate markdown file
printTable: true, // Show detailed console table
sortOrder: 'completed-first' // Sort completed operations first
})
hljs 📊 GraphQL Operations Report:
🌐 Endpoint: https://rickandmortyapi.com/graphql
┌─────────┬─────────────────┬──────────┬─────────────────────┬──────┬─────────────┐
│ (index) │ name │ type │ path │ line │ status │
├─────────┼─────────────────┼──────────┼─────────────────────┼──────┼─────────────┤
│ 0 │ 'getCharacters' │ 'Query' │ 'src/App.tsx' │ 15 │ '✅ Found' │
│ 1 │ 'getLocations' │ 'Query' │ 'Not found' │ -1 │ '❌ Not found' │
│ 2 │ 'createUser' │ 'Mutation'│ 'src/UserForm.tsx' │ 23 │ '✅ Found' │
└─────────┴─────────────────┴──────────┴─────────────────────┴──────┴─────────────┘
✅ Found: 2
❌ Not found: 1
📊 Total operations: 3
The tool generates a comprehensive markdown report:
# GraphQL Operations Report
Generated on: 2025-01-25T10:30:00.000Z
## Summary
- ✅ **Implemented**: 2
- ❌ **Not Implemented**: 1
- 📊 **Total Operations**: 3
## Queries
- [x] **getCharacters** ✅ - `src/App.tsx:15`
- [ ] **getLocations** ❌
## Mutations
- [x] **createUser** ✅ - `src/UserForm.tsx:23`
## Detailed Table
| Operation | Type | Status | Location | Line |
|-----------|------|--------|----------|------|
| getCharacters | Query | ✅ Found | src/App.tsx | 15 |
| getLocations | Query | ❌ Not Found | Not found | - |
| createUser | Mutation | ✅ Found | src/UserForm.tsx | 23 |
---
*Report generated by vite-graphql-usages plugin*
export type SchemaSource = SchemaSdlSource | SchemaEndpointSource;
type SchemaSdlSource = {
sdlPath: string;
endpoint?: never
};
type SchemaEndpointSource = {
sdlPath?: never;
endpoint: string
};
export interface GraphQLUsageOptions {
schemaSource: SchemaSource;
include?: string | RegExp | Array<string | RegExp>;
exclude?: string | RegExp | Array<string | RegExp>;
queryDirectory?: string;
outputFileName?: string;
saveReport?: boolean; // Whether to save markdown report (default: true)
printTable?: boolean; // Whether to print detailed table in console
}
export type QueryInfo = {
name: string;
type: string;
path: string;
line: number;
found: boolean;
};
viteGraphQLUsages(options: GraphQLUsageOptions): Plugin
Creates a Vite plugin for GraphQL usage analysis.
analyzeGraphQLUsage(options: AnalyzeOptions): Promise<QueryInfo[]>
Standalone function for analyzing GraphQL usage (used by CLI).
writeGraphQLReportToMarkdown(queries: QueryInfo[], outputPath: string, options?: ReportOptions): Promise<void>
Generates a markdown report from analysis results.
sortQueriesByImplementationStatus(queries: QueryInfo[]): QueryInfo[]
Sorts queries with implemented ones first, unimplemented ones last.
Schema Introspection: The tool connects to your GraphQL endpoint or reads your SDL file to discover all available operations (queries, mutations, subscriptions).
Code Analysis: It scans your source code files looking for references to these operation names using simple string matching.
Status Tracking: Each operation is marked as "found" or "not found" along with its location (file path and line number).
Report Generation: Results are displayed in the console and optionally written to a markdown file with checkboxes and detailed tables.
hljs src/pkg/
├── vite-graphql-usages.ts # Main Vite plugin
├── analyzer.ts # Standalone analysis logic
├── cli.ts # Command-line interface
├── introspec.ts # Schema introspection utilities
├── introspect-api.ts # GraphQL endpoint introspection
├── introspect-sdl.ts # SDL file introspection
└── helpers/
├── fs.ts # File system utilities
└── array-shift.ts # Array sorting utilities
// vite.config.ts
import viteGraphQLUsages from 'vite-plugin-graphql-usage';
export default defineConfig({
plugins: [
viteGraphQLUsages({
schemaSource: {
endpoint: 'https://rickandmortyapi.com/graphql'
}
})
]
});
// For projects using Apollo Client
viteGraphQLUsages({
schemaSource: {
endpoint: 'https://api.spacex.land/graphql/'
},
include: ['src/**/*.ts', 'src/**/*.tsx'],
exclude: ['src/**/*.test.*', 'src/**/*.stories.*'],
outputFileName: 'spacex-operations.md'
})
// Using a local schema.graphql file
viteGraphQLUsages({
schemaSource: {
sdlPath: './graphql/schema.graphql'
},
queryDirectory: './src/graphql/operations',
outputFileName: 'local-schema-usage.md'
})
# Basic analysis using GraphQL endpoint
npx graphql-usage-analyzer analyze --endpoint https://rickandmortyapi.com/graphql
# Using SDL file
npx graphql-usage-analyzer analyze --sdl ./schema.graphql
# With custom patterns
npx graphql-usage-analyzer analyze --endpoint <url> --include "**/*.ts,**/*.tsx" --exclude "**/test/**"
# Custom output location
npx graphql-usage-analyzer analyze --sdl schema.graphql --output reports/usage.md
# Analyze different directory
npx graphql-usage-analyzer analyze --endpoint <url> --directory /path/to/project
Option | Short | Description | Default |
---|---|---|---|
--endpoint |
-e |
GraphQL endpoint URL | - |
--sdl |
-s |
Path to SDL file | - |
--include |
-i |
File patterns to include | **/*.ts,**/*.tsx |
--exclude |
-x |
File patterns to exclude | node_modules/** |
--output |
-o |
Output file path | ./graphql-usage-report.md |
--directory |
-d |
Project directory | process.cwd() |
--sort |
-s |
Sort order: 'completed-first', 'uncompleted-first', or 'original' | original |
Use Specific Patterns: Configure include/exclude patterns to focus on your actual source code files.
Regular Analysis: Run the tool regularly to track implementation progress and identify unused operations.
Team Reports: Share the generated markdown reports with your team to coordinate GraphQL implementation efforts.
Schema Evolution: Use the tool when updating your GraphQL schema to see which new operations need implementation.
Cleanup: Identify unused operations that can be removed from your schema to keep it lean.
"No operations found"
"Files not being analyzed"
"Operations not detected"
Add console logs to see what's happening:
viteGraphQLUsages({
// ... config
debug: true // If you add this option to the plugin
})
This tool is part of a larger GraphQL development workflow. Feel free to extend it with additional features like:
MIT License - see LICENSE file for details.